clear
set more off


* -----------------------------------------------------------

use "${final}/oisl_clean_data.dta", clear



global outcomes any_mob sum_mob num_mob  savings	late_repayment

global outcomes1 any_mob any_deposit any_withdraw  	any_transfer any_airtime 
global outcomes2 sum_mob sum_deposit sum_withdrawal sum_transfer sum_airtime net_mob 
global outcomes3 num_mob num_deposit num_withdrawal num_transfer num_airtime	
global outcomes4_2 late_repayment 

global outcomes5 trans_month 
destring id, replace
xtset id month



est clear

local i = 1
foreach var in $outcomes{
	tempfile tf`i' 
	eststo: xtreg `var' `var'_base treatment_2 treatment_3 $controls /*
	*/					i.strata i.month, vce(cluster id)
	estadd local Wave "\text{Yes}"
	estadd local Base "\text{Yes}"
	quietly test treatment_2 = treatment_3
	estadd scalar test = r(p)	

	parmest, format(p %12.9f) saving(`tf`i'',replace) idn(`i')	
	local i = `i' + 1

}

local j = 1
foreach var in any_mob sum_mob num_mob savings late_repayment{
	quietly summ `var' if treatment == 0  & month == 8
	estadd scalar r(mean) : est`j'
	local j = `j' + 1
}


// Matrices shere we will store q-values:
foreach x of numlist 1/5{
	matrix define Tq`x' = (.,.)
	matrix colnames Tq`x' = treatment_2 treatment_3

}

preserve
// Upstream Outcomes:
dsconcat `tf1' `tf2' `tf3'  `tf4' `tf5'
keep if parm == "treatment_2"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
format q   %12.8fc

foreach x of numlist 1/5 {
matrix Tq`x'[1,1] = (q[`x'])
}

dsconcat `tf1' `tf2' `tf3'  `tf4' `tf5'
keep if parm == "treatment_3"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
format q   %12.8fc

foreach x of numlist 1/5 {
matrix Tq`x'[1,2] = (q[`x'])
}

foreach x of numlist 1/5{
estadd matrix Tq = Tq`x' : est`x'
}

restore

esttab est1 est2 est3 est4 est5 using "${output}/oisl_main.tex", replace /*
*/ 	   sfmt(%12.3fc) number booktabs b(3) se(3) label /*
*/	   star(* 0.1 ** 0.05 *** 0.01)				/*
*/	   depvars keep(treatment_2 treatment_3)    /*
*/	   coeflabel(treatment_2 	"Saving " /*
*/				 treatment_3 	"Mobile Banking ") 		/*
*/	   cells(b(fmt(%12.3fc)) se(fmt(%12.3fc) par) /*
*/			 p(star par([ ])) /*
*/			 Tq(star par(\{ \}) pvalue(Tq)))  /*
*/	   mtitles("Any use" "Value transactions" "Number transactions" "Savings Balance" "Late Loan")   /*
*/	   collabels(none) compress alignment(c)					/*
*/	   scalars("N Observations" "mean Control Mean" /*
*/			   "test p-Value Saving=MB " "Wave Wave FE") /*
*/	   title(Effect of Mobile Banking on on mobile banking use, savings and loans \label{tab:res1}) /*
*/	   addnotes("All regressions control for strata fixed effects, wave fixed effects and the following controls selected by post-double-select LASSO: age, age squared, gender, saving balance and saving balance squared. Any use is a dummy variable equal to one if any mobile banking transaction was carried out during the month. Value transactions is the sum of the value of mobile banking transactions in a month, in Ghanian cedis. Number transactions is the number of mobile banking transactions carried out in a month. Savings balance is the end of month balance in GHS in the partner bank account only. Late loan payment is a dummy variable if the client has missed the a loan repayment deadline. It is missing for non-loan clients. Control mean is the mean for the outcome variable in the control group in August. Individually clustered standard errors in parentheses. p-values in square brackets. q-values in curly brackets. \sym{ ***} p $<$ 0.01, \sym{**} p $<$ 0.05, \sym{*}* p $<$ 0.1." ) substitute({l} {p{1\linewidth}}) 



est clear

local i = 1
foreach var in $outcomes{
	tempfile tf`i' 
	eststo: xtreg `var' `var'_base treatment_2 treatment_3 /*
	*/					i.strata i.month, vce(cluster id)
	estadd local Wave "\text{Yes}"
	estadd local Base "\text{Yes}"
	quietly test treatment_2 = treatment_3
	estadd scalar test = r(p)	

	parmest, format(p %12.9f) saving(`tf`i'',replace) idn(`i')	
	local i = `i' + 1

}

local j = 1
foreach var in any_mob sum_mob num_mob savings late_repayment{
	quietly summ `var' if treatment == 0  & month == 8
	estadd scalar r(mean) : est`j'
	local j = `j' + 1
}


// Matrices shere we will store q-values:
foreach x of numlist 1/5{
	matrix define Tq`x' = (.,.)
	matrix colnames Tq`x' = treatment_2 treatment_3

}

preserve
// Upstream Outcomes:
dsconcat `tf1' `tf2' `tf3'  `tf4' `tf5'
keep if parm == "treatment_2"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
format q   %12.8fc

foreach x of numlist 1/5 {
matrix Tq`x'[1,1] = (q[`x'])
}

dsconcat `tf1' `tf2' `tf3'  `tf4' `tf5'
keep if parm == "treatment_3"
keep p idnum
qqvalue p, method(yekutieli) qvalue(q)
format q   %12.8fc

foreach x of numlist 1/5 {
matrix Tq`x'[1,2] = (q[`x'])
}

foreach x of numlist 1/5{
estadd matrix Tq = Tq`x' : est`x'
}

restore

esttab est1 est2 est3 est4 est5 using "${output}/oisl_main_nc.tex", replace /*
*/ 	   sfmt(%12.3fc) number booktabs b(3) se(3) label /*
*/	   star(* 0.1 ** 0.05 *** 0.01)				/*
*/	   depvars keep(treatment_2 treatment_3)    /*
*/	   coeflabel(treatment_2 	"Saving " /*
*/				 treatment_3 	"Mobile Banking ") 		/*
*/	   cells(b(fmt(%12.3fc)) se(fmt(%12.3fc) par) /*
*/			 p(star par([ ])) /*
*/			 Tq(star par(\{ \}) pvalue(Tq)))  /*
*/	   mtitles("Any use" "Value transactions" "Number transactions" "Savings Balance" "Late Loan")   /*
*/	   collabels(none) compress alignment(c)					/*
*/	   scalars("N Observations" "mean Control Mean" /*
*/			   "test p-Value Saving=MB " "Wave Wave FE") /*
*/	   title(Effect of Mobile Banking on on mobile banking use, savings and loans - no controls \label{tab:res1_0}) /*
*/	   addnotes("All regressions control for strata fixed effects, wave fixed effects and the baseline value of the outcome of interest. Any use is a dummy variable equal to one if any mobile banking transaction was carried out during the month. Value transactions is the sum of the value of mobile banking transactions in a month, in Ghanian cedis. Number transactions is the number of mobile banking transactions carried out in a month. Savings balance is the end of month balance in GHS in the partner bank account only. Late loan payment is a dummy variable if the client has missed the a loan repayment deadline. It is missing for non-loan clients. Control mean is the mean for the outcome variable in the control group in August. p-value Saving=MB is the p-value from a t-test of the equality of the Mobile Banking and Saving coefficients. Individually clustered standard errors in parentheses. p-values in square brackets. q-values in curly brackets. \sym{ ***} p $<$ 0.01, \sym{**} p $<$ 0.05, \sym{*}* p $<$ 0.1." ) substitute({l} {p{1\linewidth}}) 

 
est clear
foreach var in $outcomes1{

// Omit outcomes at baseline due to collinearity
	eststo: xtreg `var' treatment_2 treatment_3 /*
	*/				    i.strata i.month $controls, 		/*
	*/					vce(cluster id)
	estadd local Base 	"No"
	estadd local SE 	"Yes"
	estadd local WE 	"Yes"
	quietly summ `var' if (treatment == 0 & month == 8)
	estadd scalar r(mean)
	quietly test treatment_2 = treatment_3
	estadd scalar test = r(p)
	
}

esttab using "${output}/oisl_any.tex", replace /*
*/	   sfmt(%12.3fc) number booktabs f b(3) se(3) /*
*/	   star(* 0.1 ** 0.05 *** 0.01)/*
*/	   keep(treatment_3 treatment_2) /*
*/	   coeflabel(treatment_2 "Saving" treatment_3 "Mobile Banking") /*
*/	   label depvars /*
*/	   mtitles("Any MoB" "Any Deposit" "Any Withdrawal" /*
*/			   "Any Transfer" "Any Airtime")/*
*/     collabels(none) compress /*
*/	   scalars("N Observations" "mean Control Mean" /*
*/			   "test p-Value Saving=MB"/*
*/			    "WE Wave FE") 

est clear
foreach var in $outcomes2{

// Omit outcomes at baseline due to collinearity
	eststo: xtreg `var' treatment_2 treatment_3 /*
	*/				    i.strata i.month $controls, 		/*
	*/					vce(cluster id)
	estadd local Base 	"No"
	estadd local SE 	"Yes"
	estadd local WE 	"Yes"	
	quietly summ `var' if (treatment == 0 & month == 8)
	estadd scalar r(mean)
	quietly test treatment_2 = treatment_3
	estadd scalar test = r(p)	
	
	
}

esttab using "${output}/oisl_sum.tex", replace /*
*/	   sfmt(%12.3fc) number booktabs f b(3) se(3) /*
*/	   star(* 0.1 ** 0.05 *** 0.01)/*
*/	   keep(treatment_3 treatment_2) /*
*/	   coeflabel(treatment_2 "Saving" treatment_3 "Mobile Banking") /*
*/	   label depvars /*
*/	   mtitles("Value total" "Value Deposit" "Value Withdrawal"  "Value Transfer" "Value Airtime" "Net Deposit Value" )/*
*/     collabels(none) compress /*
*/	   scalars("N Observations" "mean Control Means" /*
*/			   "test p-Value Saving=MB"/*
*/			    "WE Wave FE") 

*graphs 

cumul 	net_mob if treatment==0 & any_mob==1 & net_mob<5000, 	gen(cumul1)
cumul 	net_mob if treatment==1 & any_mob==1 & net_mob<5000, 	gen(cumul2)
cumul 	net_mob if treatment==2	& any_mob==1 & net_mob<5000, 	gen(cumul3)

global 	StarSize 	= 1.5
twoway 	(line cumul1 net_mob	if any_mob==1 & net_mob<5000, lpattern(solid) lcolor(green%60) sort) ///
(line cumul2 net_mob	if any_mob==1 & net_mob<5000, lpattern(shortdash) lcolor(blue%60) sort) ///
(line cumul3 net_mob	if any_mob==1 & net_mob<5000, lpattern(longdash) lcolor(red%60) sort), /// 
 ytitle("Percentile") ylabel(0.05 0.10 0.25 0.50 0.75 0.90 0.95)  legend(label(1 "Control") label(2 "Saving") label(3 "Mobile Banking")  ) graphregion(fcolor(white) lcolor(white)) plotregion(fcolor(white) icolor(white) ilstyle(none) lcolor(white))  ylabel(,grid) 
graph export "$output/net_deposits_cumulative.png", replace
drop cumul*

est clear
foreach var in $outcomes3{

// Omit outcomes at baseline due to collinearity
	eststo: xtreg `var' treatment_2 treatment_3 /*
	*/				    i.strata i.month $controls, 		/*
	*/					vce(cluster id)
	estadd local Base 	"No"
	estadd local SE 	"Yes"
	estadd local WE 	"Yes"
	quietly summ `var' if (treatment == 0  & month == 8)
	estadd scalar r(mean)
	quietly test treatment_2 = treatment_3
	estadd scalar test = r(p)	
	
}

esttab using "${output}/oisl_no.tex", replace /*
*/	   sfmt(%12.3fc) number booktabs f b(3) se(3) /*
*/	   star(* 0.1 ** 0.05 *** 0.01)/*
*/	   keep(treatment_3 treatment_2) /*
*/	   coeflabel(treatment_2 "Saving" treatment_3 "Mobile Banking") /*
*/	   label depvars /*
*/	   mtitles("No. MoB" "No. Deposit" "No. Withdrawal" /*
*/			   "No. Transfer" "No. Airtime")/*
*/     collabels(none) compress /*
*/	   scalars("N Observations" "mean Control Mean" /*
*/			   "test p-Value Saving=MB"/*
*/			    "WE Wave FE") 

est clear
foreach var in $outcomes4_1{

	eststo: xtreg `var' treatment_2 treatment_3 /*
	*/				    `var'_base i.strata i.month $controls, 		/*
	*/					vce(cluster id)
	estadd local Base 	"Yes"
	estadd local SE 	"Yes"
	estadd local WE 	"Yes"
	quietly summ `var' if (treatment == 0 & month == 8)
	estadd scalar r(mean)
	quietly test treatment_2 = treatment_3
	estadd scalar test = r(p)	
	
	
}


est clear 
foreach var in $outcomes5{

	eststo: xtreg `var' treatment_2 treatment_3 /*
	*/				    `var'_base i.strata i.month $controls, 		/*
	*/					vce(cluster id)
	estadd local Base 	"Yes"
	estadd local SE 	"Yes"
	estadd local WE 	"Yes"
	quietly summ `var' if (treatment == 0  & month == 8)
	estadd scalar r(mean)
	quietly test treatment_2 = treatment_3
	estadd scalar test = r(p)	
	
	
}

esttab using "${output}/oisl_transaction.tex", replace /*
*/	   sfmt(%12.3fc) number booktabs f b(3) se(3) /*
*/	   star(* 0.1 ** 0.05 *** 0.01)/*
*/	   keep(treatment_3 treatment_2) /*
*/	   coeflabel(treatment_2 "Saving" treatment_3 "Mobile Banking") /*
*/	   label depvars /*
*/	   mtitles("Any bank transaction")/*
*/     collabels(none) compress /*
*/	   scalars("N Observations" "mean Control Mean" /*
*/			   "test p-Value Saving=MB"/*
*/			    "WE Wave FE") 


// Specification 3:
est clear
foreach var in $outcomes{

// Omit outcomes at baseline due to collinearity
	eststo: xtreg `var' `var'_base i.month##treatment_2 i.month##treatment_3  /*
	*/				    i.strata i.month $controls, 		/*
	*/					vce(cluster id)
	estadd local Base 	"No"
	estadd local SE 	"Yes"
	estadd local Saving ""
	estadd local Mobile Banking ""
	quietly summ `var' if (treatment == 0  & month == 8)
	estadd scalar r(mean)
	quietly test 1.treatment_2 = 1.treatment_3
	estadd scalar p_t = r(p)
	quietly test 9.month#1.treatment_2 = 10.month#1.treatment_2
	estadd scalar p_sep_oct = r(p)
	quietly test 9.month#1.treatment_2 = 11.month#1.treatment_2
	estadd scalar p_sep_nov = r(p)
	quietly test 10.month#1.treatment_2 = 11.month#1.treatment_2
	estadd scalar p_oct_nov = r(p)
	quietly test 9.month#1.treatment_3 = 10.month#1.treatment_3
	estadd scalar t_sep_oct = r(p)
	quietly test 9.month#1.treatment_3 = 11.month#1.treatment_3
	estadd scalar t_sep_nov = r(p)
	quietly test 10.month#1.treatment_3 = 11.month#1.treatment_3
	estadd scalar t_oct_nov = r(p)
	
	
}

esttab using "${output}/oisl_main_time.tex", replace /*
*/	   sfmt(%12.3fc) number booktabs f b(3) se(3) /*
*/	   star(* 0.1 ** 0.05 *** 0.01)/*
*/	   label depvars collabels(none) compress /*
*/	   keep(1.treatment_3 9.month#1.treatment_3 10.month#1.treatment_3 /*
*/		    11.month#1.treatment_3 /*
*/			1.treatment_2 9.month#1.treatment_2 10.month#1.treatment_2 /*
*/		    11.month#1.treatment_2) /*
*/	   coeflabel(1.treatment_2 		    "Saving"	/*
*/				 9.month#1.treatment_2  "$\text{     }$ $\times$ September" /*
*/				 10.month#1.treatment_2 "$\text{     }$ $\times$ October" 	/*
*/				 11.month#1.treatment_2 "$\text{     }$ $\times$ November" 	/*
*/				 1.treatment_3 			"Mobile Banking" 	/*
*/				 9.month#1.treatment_3  "$\text{     }$ $\times$ September" /*
*/				 10.month#1.treatment_3 "$\text{     }$ $\times$ October" 	/*
*/				 11.month#1.treatment_3 "$\text{     }$ $\times$ November") /*
*/	   refcat(9.month#1.treatment_2 "Saving" /*
*/			  1.treatment_3 "\vspace{0.1em}" /*
*/			  9.month#1.treatment_3 "Mobile Banking", nolabel) /*
*/	   mtitles("Any MoB" "Sum MoB" "No. MoB") /*
*/	   scalars("p_t Saving = Mobile Banking" "placebo Saving" "p_sep_oct \hspace{1em}September = October"/*
*/			   "p_sep_nov \hspace{1em}September = November" "p_oct_nov \hspace{1em}October = November"/*
*/			"treatment Mobile Banking"   "t_sep_oct \hspace{1em}September = October" "t_sep_nov \hspace{1em}September = November"/*
*/			   "t_oct_nov \hspace{1em}October = November"/*
*/			   "N Observations" "mean Control Mean"/*
*/			   ) 




	
// Number of messages listened to 
tab complete_calc, gen(complete_calc_only_)

est clear
foreach var in any_mob sum_mob num_mob savings{
	eststo: xtreg `var' treatment_3 complete_calc_only_2##treatment_3 complete_calc_only_3##treatment_3 complete_calc_only_4##treatment_3 complete_calc_only_5##treatment_3 complete_calc_only_6##treatment_3 complete_calc_only_7##treatment_3 complete_calc_only_7##treatment_3 complete_calc_only_8##treatment_3 complete_calc_only_9##treatment_3 complete_calc_only_10##treatment_3 complete_calc_only_11##treatment_3 `var'_base i.strata i.month if treatment != 0, vce(cluster id) noomitted noemptycells
	estadd local Base "No" //Update for Savings
	estadd local WE "Yes"
	estadd local SE "Yes"
	quietly summ `var' if treatment == 1 & month == 8
	estadd scalar r(mean)
}
	eststo: xtreg late_repayment treatment_3 complete_calc_only_2##treatment_3 complete_calc_only_3##treatment_3 complete_calc_only_4##treatment_3 complete_calc_only_5##treatment_3 complete_calc_only_6##treatment_3 complete_calc_only_7##treatment_3 complete_calc_only_7##treatment_3 complete_calc_only_8##treatment_3 complete_calc_only_9##treatment_3 complete_calc_only_10##treatment_3 complete_calc_only_11##treatment_3 late_repayment_base i.strata i.month if treatment != 0, vce(cluster id) noomitted noemptycells
	estadd local Base "Yes"
	estadd local WE "Yes"
	estadd local SE "Yes"
	quietly summ late_repayment if treatment == 1 & strata == 3 & month == 8
	estadd scalar r(mean)

esttab using "${output}/messages_needed.tex", replace /*
*/	   sfmt(%12.3fc) number booktabs f b(3) se(3) label depvars/*
*/	   star(* 0.1 ** 0.05 *** 0.01)							/*
*/ 	   mtitles("Any use" "Value transactions" "Number transactions" "Savings (GHS)" "Late Repayment") /*
*/	   keep(treatment_3/*
*/			1.complete_calc_only_2#1.treatment_3 /*
*/			1.complete_calc_only_3#1.treatment_3 /*
*/			1.complete_calc_only_4#1.treatment_3 /*
*/			1.complete_calc_only_5#1.treatment_3 /*
*/			1.complete_calc_only_6#1.treatment_3 /*
*/			1.complete_calc_only_7#1.treatment_3 /*
*/			1.complete_calc_only_8#1.treatment_3 /*
*/			1.complete_calc_only_9#1.treatment_3 /*
*/			1.complete_calc_only_10#1.treatment_3 /*
*/			1.complete_calc_only_11#1.treatment_3) /*
*/	   order(treatment_3  /*
*/			1.complete_calc_only_2#1.treatment_3 /*
*/			1.complete_calc_only_3#1.treatment_3 /*
*/			1.complete_calc_only_4#1.treatment_3 /*
*/			1.complete_calc_only_5#1.treatment_3 /*
*/			1.complete_calc_only_6#1.treatment_3 /*
*/			1.complete_calc_only_7#1.treatment_3 /*
*/			1.complete_calc_only_8#1.treatment_3 /*
*/			1.complete_calc_only_9#1.treatment_3 /*
*/			1.complete_calc_only_10#1.treatment_3 /*
*/			1.complete_calc_only_11#1.treatment_3) /*
*/	   coeflabel(1.complete_calc_only_2#1.treatment_3 "$\text{     } \times$ Only 1"/*
*/	1.complete_calc_only_3#1.treatment_3 "$\text{     } \times$ Only 2"/* 
*/	1.complete_calc_only_4#1.treatment_3 "$\text{     } \times$ Only 3"/*
*/	1.complete_calc_only_5#1.treatment_3 "$\text{     } \times$ Only 4"/*
*/	1.complete_calc_only_6#1.treatment_3 "$\text{     } \times$ Only 5"/*
*/	1.complete_calc_only_7#1.treatment_3 "$\text{     } \times$ Only 6"/*
*/	1.complete_calc_only_8#1.treatment_3 "$\text{     } \times$ Only 7" /*
*/	1.complete_calc_only_9#1.treatment_3 "$\text{     } \times$ Only 8"/*
*/	1.complete_calc_only_10#1.treatment_3 "$\text{     } \times$ Only 9"/*
*/	1.complete_calc_only_11#1.treatment_3 "$\text{     } \times$ All messages")/*
*/	   refcat(1.complete_calc_only_2#1.treatment_3 "Mobile Banking", nolabel)/*
*/     collabels(none) compress /*
*/	   scalars("N Observations" "mean Saving Mean" /*
*/			    "WE Wave FE") 

coefplot est1, keep(1.complete_calc_only_2#1.treatment_3 /*
		*/			1.complete_calc_only_3#1.treatment_3 /*
		*/			1.complete_calc_only_4#1.treatment_3 /*
		*/			1.complete_calc_only_5#1.treatment_3 /*
		*/			1.complete_calc_only_6#1.treatment_3 /*
		*/			1.complete_calc_only_7#1.treatment_3 /*
		*/			1.complete_calc_only_8#1.treatment_3 /*
		*/			1.complete_calc_only_9#1.treatment_3 /*
		*/			1.complete_calc_only_10#1.treatment_3 /*
		*/			1.complete_calc_only_11#1.treatment_3) /*
		*/			vertical title("Any use") graphr(fc(white))/*
		*/			ylabel(, glcolor(black%10)) subtitle("Main Pitch")/*
*/coeflabels(1.complete_calc_only_2#1.treatment_3 = "Only 1" /*
*/			 1.complete_calc_only_3#1.treatment_3 = "Only 2" /*
*/			 1.complete_calc_only_4#1.treatment_3 = "Only 3" /*
*/			 1.complete_calc_only_5#1.treatment_3 = "Only 4" /*
*/			 1.complete_calc_only_6#1.treatment_3 = "Only 5" /*
*/			 1.complete_calc_only_7#1.treatment_3 = "Only 6" /*
*/			 1.complete_calc_only_8#1.treatment_3 = "Only 7" /*
*/			 1.complete_calc_only_9#1.treatment_3 = "Only 8" /*
*/			 1.complete_calc_only_10#1.treatment_3 = "Only 9" /*
*/			 1.complete_calc_only_11#1.treatment_3 = "All") 
graph export "$output/any_mob_treatment_x_messages_pitch.pdf", replace
